package com.stone.framework.user.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.stone.framework.core.context.UserContext;
import com.stone.framework.core.exception.BusinessException;
import com.stone.framework.message.PageReqMessage;
import com.stone.framework.message.PageResMessage;
import com.stone.framework.message.ReqMessage;
import com.stone.framework.message.ResMessage;
import com.stone.framework.message.user.MenuResBody;
import com.stone.framework.message.user.ResourcePageReqBody;
import com.stone.framework.message.user.ResourcePageResBody;
import com.stone.framework.message.user.ResourceReqBody;
import com.stone.framework.message.user.ResourceTreeResBody;
import com.stone.framework.user.service.ResourceService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "资源")
@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @ApiOperation(value = "获取树形结构", httpMethod = "POST")
    @PostMapping(value = "/getTree")
    ResMessage<List<ResourceTreeResBody>> getTree(@Valid @RequestBody ReqMessage<Void> reqMessage) {
        List<ResourceTreeResBody> list = resourceService.buildTree();
        return ResMessage.success(list);
    }

    @ApiOperation(value = "获取菜单", httpMethod = "POST")
    @PostMapping(value = "/getMenu")
    ResMessage<List<MenuResBody>> getMenu(@Valid @RequestBody ReqMessage<Void> reqMessage) {
        String username = UserContext.getUsername().orElseThrow(() -> new BusinessException("无法获取登录人信息"));
        List<MenuResBody> list = resourceService.buildMenu(username);
        return ResMessage.success(list);
    }

    /**
     * 暂无调用场景
     * 
     * @param reqMessage
     * @return
     */
    @ApiOperation(value = "查询", httpMethod = "POST", hidden = true)
    @PostMapping(value = "/getPage")
    PageResMessage<ResourcePageResBody> getPage(@Valid @RequestBody PageReqMessage<ResourcePageReqBody> reqMessage) {
        IPage<ResourcePageResBody> page = resourceService.queryPage(reqMessage.getBody(), reqMessage.buildPage());
        return PageResMessage.success(page);
    }

    @ApiOperation(value = "新增", httpMethod = "POST")
    @PostMapping(value = "/add")
    ResMessage<Void> add(@Valid @RequestBody ReqMessage<ResourceReqBody> reqMessage) {
        resourceService.save(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "修改", httpMethod = "POST")
    @PostMapping(value = "/update")
    ResMessage<Void> update(@Valid @RequestBody ReqMessage<ResourceReqBody> reqMessage) {
        resourceService.modify(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "刪除", httpMethod = "POST")
    @PostMapping(value = "/delete")
    ResMessage<Void> delete(@Valid @RequestBody ReqMessage<Long> reqMessage) {
        resourceService.delete(reqMessage.getBody());
        return ResMessage.success();
    }

}
